// components/dd-indexes/index.js
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    list: {type: Array, value: []},
  },

  /**
   * 组件的初始数据
   */
  data: {
    StatusBar: 0,
    CustomBar: getApp().globalData.CustomBar,
    hidden: true,
    keyword: '',
  },

  /**
   * 组件的方法列表
   */
  methods: {
    bindKeyInput: function (e) {
      this.setData({
        keyword: e.detail.value
      })
    },
    onClickSearch() {
      this.triggerEvent('clickSearch', this.data.keyword)
    },
    onClickAvatar(event) {
      const data = event.currentTarget.dataset.operation;
      this.triggerEvent('clickAvatar', data)
    },
    //获取文字信息
    getCur(e) {
      this.setData({
        hidden: false,
        listCur: this.data.list[e.target.id].key,
      })
    },
    setCur(e) {
      this.setData({
        hidden: true,
        listCur: this.data.listCur
      })
    },
    //滑动选择Item
    tMove(e) {
      let y = e.touches[0].clientY,
        offsettop = this.data.boxTop,
        that = this;
      //判断选择区域,只有在选择区才会生效
      if (y > offsettop) {
        let num = parseInt((y - offsettop) / 20);
        this.setData({
          listCur: that.data.list[num]
        })
      };
    },
    //触发全部开始选择
    tStart() {
      this.setData({
        hidden: false
      })
    },
    //触发结束选择
    tEnd() {
      this.setData({
        hidden: true,
        listCurID: this.data.listCur
      })
    },
    indexSelect(e) {
      let that = this;
      let barHeight = this.data.barHeight;
      let list = this.data.list;
      let scrollY = Math.ceil(list.length * e.detail.y / barHeight);
      for (let i = 0; i < list.length; i++) {
        if (scrollY < i + 1) {
          that.setData({
            listCur: list[i],
            movableY: i * 20
          })
          return false
        }
      }
    }
  },
  lifetimes: {
    attached: function () {
      this.setData({
        listCur: this.properties.list[0]
      })
    },
    ready: function () {
      let that = this;
      this.createSelectorQuery().select('.indexBar-box').boundingClientRect(function (res) {
        that.setData({
          boxTop: res?.top
        }) 
      }).exec();
      this.createSelectorQuery().select('.indexes').boundingClientRect(function (res) {
        that.setData({
          barTop: res?.top
        })
      }).exec()
    },
    detached: function () {
    },
  },
})